﻿using AOT;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Rendering;
using static TMPro.SpriteAssetUtilities.TexturePacker_JsonArray;

public class test : MonoBehaviour
{

    static int mediaid=0;
    public MeshRenderer target;
    // Start is called before the first frame update
    void Start()
    {
       
    }
    Texture2D texbuf;
    // Update is called once per frame
    void Update()
    {
        if (mediaid >0)
        {
            var w = MediaSystemBind.Media_GetViewWidth(mediaid);
            var h = MediaSystemBind.Media_GetViewHeight(mediaid);
            if (w < 1 | h < 1)
                return;

            if (texbuf == null || texbuf.width != w || texbuf.height != h)
            {
                Debug.LogWarning("==== init texture =" + w + "," + h);
                if (texbuf != null)
                {
                    Texture2D.DestroyImmediate(texbuf);
                    texbuf = null;
                }
                texbuf = new Texture2D(w, h, TextureFormat.RGBA32, false);
                texbuf.Apply(false, false);
              

            }
            var buffer = MediaSystemBind.Media_GetViewBuffer(mediaid);

            texbuf.LoadRawTextureData(buffer, texbuf.width * texbuf.height * 4);
            texbuf.Apply(false, false);
            Debug.LogWarning("Intptr:" + buffer.ToString("X08"));
        }
        if (target != null && texbuf != null)
        {
            target.material.mainTexture = texbuf;
            Debug.LogWarning("==== update Tex");
        }
    }

    private void OnGUI()
    {
        float scale = 2;
#if UNITY_EDITOR
#else
    scale=3.0f;
#endif
        GUIUtility.ScaleAroundPivot(new Vector2(scale, scale), Vector2.zero);
        GUILayout.Label("Video Test.");

        if(MediaSystemBind.Support)
        {
            GUILayout.Label("Support");
        }
        else
        {
            GUILayout.Label("Not Support.Android Runtime Only.");
            return;
        }
        if (mediaid>0)
        {
            GUILayout.Label("State=" + MediaSystemBind.Media_GetViewWidth(mediaid) + "," + MediaSystemBind.Media_GetViewHeight(mediaid));
        }
        else
        {
            GUILayout.Label("Player release");

        }
        if (GUILayout.Button("Open&Play"))
        {
            if (mediaid == 0)
            {
                MediaSystemBind.System_Start();
              
               
            }

            //打开媒体，参数3=true 自动播放
            mediaid= MediaSystemBind.Media_Open(MediaSystemBind.MediaMode.StramingAssets, "trailer.mp4", true);

            //打开媒体视频流
            MediaSystemBind.Media_OpenView(mediaid);

        }

        if (GUILayout.Button("Close"))
        {
            if (mediaid>0)
            {
                mediaid = 0;
                MediaSystemBind.System_Stop();
             
                Texture2D.DestroyImmediate(texbuf);
                texbuf = null;
            }
        }
    }
}
